package com.ifp.weixin.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.ifp.weixin.constant.Constant;
import org.apache.http.HttpEntity;                   
import org.apache.http.HttpResponse;                 

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;            
import org.apache.http.client.methods.HttpGet;   
import org.apache.http.client.methods.HttpPost;   
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;  
import org.apache.http.entity.StringEntity;  
import org.apache.http.util.EntityUtils;  
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.client.params.ClientPNames;

import org.apache.log4j.Logger;
import org.springframework.web.client.HttpClientErrorException;

public class HttpClientUtil {
	public static Logger log = Logger.getLogger(HttpClientUtil.class);
	public static String getHttp(String url){
	    String responseMsg = "";

	    // 1.构�?�HttpClient的实�?
	    DefaultHttpClient httpClient = new DefaultHttpClient();

	    // 2.创建GetMethod的实�?
	    HttpGet getMethod = new HttpGet(url);

	    // 使用系统系统的默认的恢复策略
	    DefaultHttpRequestRetryHandler handler = new DefaultHttpRequestRetryHandler(2,true); 
	    httpClient.setHttpRequestRetryHandler(handler);
	    
	    try {
	        //3.执行getMethod,调用http接口
	    	HttpResponse response  = httpClient.execute(getMethod);
	        HttpEntity entity = response.getEntity();  
	        //4.读取内容
        	if (entity!=null ) {
        		System.out.println("Response content length: " + entity.getContentLength());
        	}
        	BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
        	String line = null;
        	StringBuffer sf = new StringBuffer();
        	while ((line = reader.readLine()) != null) {
        		System.out.println(line);
        		sf.append(line);
        	}	       
	        //5.处理返回的内�?
	        responseMsg = sf.toString( );
	        			
	        log.info(responseMsg);
	       
	    } catch (HttpClientErrorException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }finally{
	        //6.释放连接
	    	httpClient.getConnectionManager().shutdown();
	    }
	    return responseMsg;
	}

}

